
// Load Data
use statefip  labforce empstat year   educd  perwt age  puma  sex race classwkr cntygp97 cntygp98 if year == 2000 using "$dir/data/data_public/census_1970_2000.dta", clear



	// Prepare
	keep if age >=15 & age<65
	
		gen emp = (empstat == 1) if classwkr != 1 
		
		keep if emp == 1

// Commuting Zone
         // PUMAS only defined for: 1990,2000, 2005-2015.
         gen     puma2000 = real(string(statefip) + "00" + string(puma)) if puma<1000 & year>=1990 
         replace puma2000 = real(string(statefip) + "0" + string(puma)) if puma>=1000 & puma<10000 & year>=1990 
         replace puma2000 = real(string(statefip) + string(puma)) if puma>=10000 & year>=1990 

         joinby puma2000 using "$dir\data\interim\cw_cty_czone_puma2000_afact.dta" , unmatched(master)
         tab _merge
         gen merged = (_merge==3)

         drop _merge
        
	gen new_weight = perwt*afact
	
	
	// Create variables fror grouping
	gen     gr_race = 1 if race == 1
	replace gr_race = 2 if gr_race==. & race!=.


	gen     gr_educ = 1 if educ <64 // hs or less
	replace gr_educ = 2 if educ>=64 & educ<101 // some college
	replace gr_educ = 3 if educ>=101 & educ<.  // college or more      

	
	gen gr_age = 1 if age<=30
	replace gr_age = 2 if age>30 & age<40 
	replace gr_age = 3 if age>40 & age<. 
	 
	egen group = group(sex gr_race gr_educ gr_age)
	
		// Collapse
	collapse (sum) emp [pw=new_weight], by(group czone)
	
	
	egen emp_sum_czone = total(emp), by(czone)

	gen emp_share_2000  = emp/emp_sum_czone
	
	keep emp_share_2000 czone group
		
	save "$dir\data\interim\fixed_demo_group_2000.dta", replace
	
	
	
